package com.cb.ucenterservice.controller;


import com.cb.ucenterservice.entity.UcenterMember;
import com.cb.ucenterservice.entity.vo.RegisterVo;
import com.cb.ucenterservice.service.UcenterMemberService;
import com.cb.utils.JwtUtils;
import com.cb.utils.R;
import com.cb.utils.ordervo.UcenterMemberOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Cebb
 * @since 2020-06-27
 */
@Api(description = "用户管理")

@RestController
@RequestMapping("/ucenterservice/member")
public class UcenterMemberController {

    private final UcenterMemberService ucenterMemberService;

    @Autowired
    public UcenterMemberController(UcenterMemberService ucenterMemberService) {
        this.ucenterMemberService = ucenterMemberService;
    }

    @ApiOperation("登录")
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        String token = ucenterMemberService.login(member);

        return R.ok().data("token", token);
    }

    @ApiOperation("注册")
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        ucenterMemberService.register(registerVo);

        return R.ok();
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = ucenterMemberService.getById(memberId);

        return R.ok().data("member", member);
    }

    @ApiOperation("根据用户id获取用户信息")
    @PostMapping("getUserInfoOrder/{userId}")
    public UcenterMemberOrderVo getUserInfoOrder(@PathVariable("userId") String userId){
        UcenterMember member = ucenterMemberService.getById(userId);
        UcenterMemberOrderVo memberOrderVo = new UcenterMemberOrderVo();
        BeanUtils.copyProperties(member, memberOrderVo);

        return memberOrderVo;
    }

    @ApiOperation("查询某天的注册人数")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day){
        Integer count = ucenterMemberService.countRegister(day);
        return R.ok().data("countRegister", count);
    }

}